package com.ahucoding.rocket.controller;

import com.ahucoding.rocket.common.model.Post;
import com.ahucoding.rocket.common.model.R;
import com.ahucoding.rocket.common.param.PostParam;
import com.ahucoding.rocket.service.PostService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.time.LocalDateTime;
import java.time.LocalDateTime;

/**
 * @Description 内容表 controller
 * @Author makejava
 * @CreateDate 2025-05-15 14:00:38
 * @Version 1.0
 */
@ApiOperation("内容表")
@RestController
@RequestMapping("/training")
public class PostController {

    /**
     * 内容表Service
     */
    @Resource
    private PostService postService;

    /**
     * 内容表新增
     *
     * @param postParam PostParam
     * @return ApiResponseBody
     */
    @ApiOperation("内容表新增")
    @PostMapping(path = "/post", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public R<?> createPost(@RequestBody PostParam postParam) {
        postService.createPost(postParam);
        return R.ok();
    }

    /**
     * 内容表修改
     *
     * @param postParam PostParam
     * @return ApiResponseBody
     */
    @ApiOperation("内容表修改")
    @PutMapping(path = "/post", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public R<?> updatePost(@RequestBody PostParam postParam) {
        postService.updatePost(postParam);
        return R.ok();
    }

    /**
     * 根据id获取内容表信息
     *
     * @param id String
     * @return ApiResponseBody
     */
    @ApiOperation("根据id获取内容表信息")
    @GetMapping(path = "/post/{id}", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public R<?> getPostById(@PathVariable("id") Long id) {
        if (id == null) {
            return R.fail("参数错误");
        }
        return R.ok(postService.getPostById(id));
    }

    /**
     * 内容表分页列表
     * 内容表分页列表
     *
     * @param currentPage  当前页
     * @param pageSize     页大小
     * @param order        排序方式
     * @param orderFields  排序字段
     * @param id           $column.comment
     * @param createBy     创建人
     * @param createDate   创建时间
     * @param updateBy     更新人
     * @param updateDate   更新时间
     * @param deleteFlag   数据状态 0可用 -1不可用
     * @param title        标题
     * @param name         名称
     * @param url          链接地址
     * @param content      内容
     * @param shortContent 内容总结
     * @param isJudge      是否经过评价
     * @param isUserDefine 是否用户定制
     * @param sourceId     源id
     * @param contentDate  内容发表时间
     * @param categoryId   类别id
     * @param stars        得分
     * @param ttl          语音文件地址
     * @return ApiResponseBody
     */
    @ApiOperation(value = "内容表分页列表")
    @GetMapping("/post/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "15"),
            @ApiImplicitParam(name = "order", value = "排序方式, desc 或 asc", defaultValue = "desc"),
            @ApiImplicitParam(name = "orderFields", value = "排序字段, create_date", defaultValue = "create_date")
            , @ApiImplicitParam(name = "id", value = "$column.comment")
            , @ApiImplicitParam(name = "createBy", value = "创建人")
            , @ApiImplicitParam(name = "createDate", value = "创建时间")
            , @ApiImplicitParam(name = "updateBy", value = "更新人")
            , @ApiImplicitParam(name = "updateDate", value = "更新时间")
            , @ApiImplicitParam(name = "deleteFlag", value = "数据状态 0可用 -1不可用")
            , @ApiImplicitParam(name = "title", value = "标题")
            , @ApiImplicitParam(name = "name", value = "名称")
            , @ApiImplicitParam(name = "url", value = "链接地址")
            , @ApiImplicitParam(name = "content", value = "内容")
            , @ApiImplicitParam(name = "shortContent", value = "内容总结")
            , @ApiImplicitParam(name = "isJudge", value = "是否经过评价")
            , @ApiImplicitParam(name = "isUserDefine", value = "是否用户定制")
            , @ApiImplicitParam(name = "sourceId", value = "源id")
            , @ApiImplicitParam(name = "contentDate", value = "内容发表时间")
            , @ApiImplicitParam(name = "categoryId", value = "类别id")
            , @ApiImplicitParam(name = "stars", value = "得分")
            , @ApiImplicitParam(name = "ttl", value = "语音文件地址")
    })
    @ResponseBody
    public R<?> page(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                     @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                     @RequestParam(value = "order", defaultValue = "desc") String order,
                     @RequestParam(value = "orderFields", defaultValue = "create_date") String orderFields
            , @RequestParam(value = "id", required = false) Long id
            , @RequestParam(value = "createBy", required = false) String createBy
            , @RequestParam(value = "createDate", required = false) LocalDateTime createDate
            , @RequestParam(value = "updateBy", required = false) String updateBy
            , @RequestParam(value = "updateDate", required = false) LocalDateTime updateDate
            , @RequestParam(value = "deleteFlag", required = false) Integer deleteFlag
            , @RequestParam(value = "title", required = false) String title
            , @RequestParam(value = "name", required = false) String name
            , @RequestParam(value = "url", required = false) String url
            , @RequestParam(value = "content", required = false) String content
            , @RequestParam(value = "shortContent", required = false) String shortContent
            , @RequestParam(value = "isJudge", required = false) Integer isJudge
            , @RequestParam(value = "isUserDefine", required = false) Integer isUserDefine
            , @RequestParam(value = "sourceId", required = false) Long sourceId
            , @RequestParam(value = "contentDate", required = false) String contentDate
            , @RequestParam(value = "categoryId", required = false) Long categoryId
            , @RequestParam(value = "stars", required = false) Integer stars
            , @RequestParam(value = "ttl", required = false) String ttl
    ) {
        return R.ok(postService.page(currentPage, pageSize, order, orderFields
                , id
                , createBy
                , createDate
                , updateBy
                , updateDate
                , deleteFlag
                , title
                , name
                , url
                , content
                , shortContent
                , isJudge
                , isUserDefine
                , sourceId
                , contentDate
                , categoryId
                , stars
                , ttl
        ));
    }

    /**
     * 内容表删除
     *
     * @param id String
     * @return ApiResponseBody
     */
    @ApiOperation("内容表删除")
    @DeleteMapping(path = "/post/{id}", produces = {"application/json;charset=UTF-8"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "日志id,多个用逗号(,)隔开")
    })
    @ResponseBody
    public R<?> deletePost(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return R.fail("参数错误");
        }
        postService.deletePost(id);
        return R.ok();
    }



}

